<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="@/assets/logo.png" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        label-width="0"
        class="login_form"
        :model="loginForm"
        :rules="loginFormRules"
        ref="formRef"
      >
        <!-- 输入用户名 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="iconfont icon-user"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <!-- 输入密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-3702mima"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- <div role="alert" class="el-message el-message--success" style="top: 82px; z-index: 2006;">
  <i class="el-message__icon el-icon-success"></i><p class="el-message__content">获取菜单列表成功</p>
  </div> -->
  </div>
</template>

<script>
  // import axios from 'axios'
  export default {
    data() {
      return {
        loginForm: {
          username: 'admin',
          password: 'admin',
        },
        // 表单的验证规则对象
        loginFormRules: {
          // 验证用户名是否合法
          username: [
            { required: true, message: '请输入用户名称', trigger: 'blur' },
            {
              min: 3,
              max: 10,
              message: '请输入3到10个字符之间字符串',
              trigger: 'blur',
            },
          ],
          // 验证密码
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            {
              min: 3,
              max: 5,
              message: '密码在6-15区间之间',
              trigger: 'blur',
            },
          ],
        },
      }
    },
    methods: {
      resetFrom() {
        this.$refs.formRef.resetFields()
      },
      login() {
        this.$refs.formRef.validate(async (val) => {
          // const { data: res } = await this.$http.post('login', this.loginForm)
          // console.log(res)
          // this.$message.error('okkkkk')
          if (!val)
            return this.$message({
              message: '登录失败',
              type: 'error',
              center: true,
            })
          if (
            this.loginForm.password == 'admin' &&
            this.loginForm.username == 'admin'
          ) {
            localStorage.setItem('token', 1)
            this.$message({
              message: '登录成功',
              type: 'success',
              center: true,
            })
          } else {
            this.$message({ message: '密码错误', type: 'error', center: true })
            localStorage.removeItem('token')
          }
          this.$router.push('/home')
        })
      },
      dl() {
        if (localStorage.getItem('token')) {
          this.$router.push('/home')
        }
      },
    },
    mounted() {
      this.dl()
    },
  }
</script>

<style lang='less' scoped>
  .login_container {
    background: #2b4b6b;
    height: 100vh;
    font-size: 18px;
    color: white;
    .login_box {
      width: 450px;
      height: 300px;
      background: white;
      border-radius: 5px;
      position: absolute;
      left: 50%;
      top: 35%;
      transform: translate(-50%, -50%);
      .avatar_box {
        width: 130px;
        height: 130px;
        border: 1px solid #eee;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px #ddd;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -40%);
        background: #fff;
        img {
          width: 100%;
          height: 100%;
          border-radius: 50%;
          background: #eee;
        }
      }
    }
  }
  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
  .btns {
    display: flex;
    justify-content: flex-end;
  }

  .el-icon-error {
    color: red !important;
  }
</style>